//
//  HomeTodayActivityTableViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/6.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit
import SnapKit
class HomeTodayActivityTableViewCell: WZC_TableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    var model:HomeFloorModel?
    {
        didSet
        {
            self.gridView.reloadData()
        }
    }
    
    var pinModel:PinRagModel?
    {
        didSet
        {
            self.gridView.reloadData()
        }
    }
    
    lazy var gridView : UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        layout.itemSize = CGSize.init(width: kScreenW/5, height:85*kHeightRatio)
        
        let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.register(TodayActivityCollectionViewCell.self, forCellWithReuseIdentifier: "TodayActivityCollectionViewCell")
        self.addSubview(collectionView)
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
 
        gridView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HomeTodayActivityTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if let tmodel = pinModel {
            
            return tmodel.items?.count ?? 0
        }
        else
        {
            
            return model?.data?.items.count ?? 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:TodayActivityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "TodayActivityCollectionViewCell", for: indexPath) as! TodayActivityCollectionViewCell
        
        if let tmodel = pinModel {
            
            cell.model = tmodel.items?[indexPath.row]
            
            let rate:CGFloat = 210.0/180.0
            
            cell.imageView.snp.updateConstraints { (make) in
              
                make.size.equalTo(CGSize(width: 50*kHeightRatio*rate, height: 50*kHeightRatio))
            }
            cell.titleLabel.snp.updateConstraints { (make) in
               make.top.equalTo(cell.imageView.snp.bottom).offset(8)
            }
        }
        else
        {
            
            cell.model = model?.data?.items[indexPath.row].item
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let tmodel = pinModel {
            
            let model1:ItemModel = tmodel.items![indexPath.item]
            let vc:WZC_WebViewController = WZC_WebViewController()
            vc.urlString = model1.href
            self.viewController?.navigationController?.pushViewController(vc, animated: true)
        }
        else
        {
            
            let model1:ItemModel = (model?.data?.items[indexPath.row].item)!
            let vc:WZC_WebViewController = WZC_WebViewController()
            vc.urlString = model1.jump_url
            self.viewController?.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}
